const Koa = require("koa");
const app = new Koa();

const router = require("./src/routes");
const { errorHandler } = require("./src/middleware/errorHandler");
const { cors } = require("./src/middleware/cors");
const { static } = require("./src/middleware/static");
const { body } = require("./src/middleware/body");
const { testConnection } = require("./src/models/db");

// 测试数据库连接
testConnection();

// 注册中间件

app.use(errorHandler()); // 错误处理中间件
app.use(cors()); // 跨域
app.use(body()); //
app.use(static()); // 静态服务目录
app.use(router.routes()); // 路由中间件

module.exports = app; // 导出app供bin/www启动
